//
//  HEHealthVideoViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/29.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthVideoViewController.h"
#import "HEVideoCell.h"
#import "HEVideoListModel.h"
#import "HEEduVideoDetailsController.h"

@interface HEHealthVideoViewController ()<UITableViewDelegate, UITableViewDataSource, HEVideoCellDelegate>

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEVideoListModel *> * dataSourceArr;

@end

@implementation HEHealthVideoViewController


- (NSMutableArray<HEVideoListModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.zx_navBar.hidden = YES;
    self.view.backgroundColor = CViewBgColor;
    
    _pageNum = 1;
    _pageSize = 20;
        
    [self setCustomTableView:self.tableView];
    [self showEmpty];
 
    [self loadData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, 0 , KScreenWidth, KScreenHeight - ZXNavBarHeight - 47) style:UITableViewStylePlain];
        _tableView.separatorColor = KHexColor(@"EEEEEE");
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        
        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            weakSelf.isFromHeadRefresh = YES;
            weakSelf.pageNum = 1;
            [weakSelf loadData];
        }];
        
        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = NO;
            weakSelf.pageNum += 1;
            [weakSelf loadData];
        }];
        
        [_tableView registerClass:[HEVideoCell class] forCellReuseIdentifier:@"HEVideoCell"];
    }
    return _tableView;
}

 
- (void)loadData {

    HEPageRequest *request = [[HEPageRequest alloc] init];
    request.pageNum = _pageNum;
    request.pageSize = _pageSize;
    request.queryParams = @{@"video_type":@"健康宣教"}.mutableCopy;

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KVideoRecommand parameters:request success:^(id  _Nonnull responseObject) {
        
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }
        
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
 
        [self.dataSourceArr addObjectsFromArray:[HEVideoListModel mj_objectArrayWithKeyValuesArray: response.data]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.count == 0) {
                [Dialog toastBottom:@"没有更多数据了~"];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEVideoCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"HEVideoCell"];
    cell.model = _dataSourceArr[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: @"HEVideoCell" cacheByIndexPath:indexPath configuration:^(HEVideoCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEVideoListModel *model = self.dataSourceArr[indexPath.row];
    
    HEEduVideoDetailsController *vc = [HEEduVideoDetailsController new];
    vc.infoId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
#pragma mark - HEVideoCellDelegate
- (void)toVideoPage:(HEVideoListModel *)model {
    HEEduVideoDetailsController *dvc = [HEEduVideoDetailsController new];
    dvc.infoId = model.id;
    [self.navigationController pushViewController:dvc animated:YES];
}

@end
